package com.bw.wxy.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.bw.wxy.R;
import com.bw.wxy.action.BroadAction;
import com.bw.wxy.common.SpUtils;
import com.bw.wxy.mvp.model.entity.BaseEntity;
import com.bw.wxy.mvp.model.entity.ResponseUserEntity;
import com.bw.wxy.factroy.HttpCode;
import com.bw.wxy.mvp.base.view.BaseFragment;
import com.bw.wxy.mvp.contract.LoginContract;
import com.bw.wxy.mvp.di.DaggerLoginComponent;
import com.bw.wxy.mvp.di.LoginModules;
import com.bw.wxy.mvp.presenter.LoginPresenter;

public class Fragment_account extends BaseFragment<LoginPresenter> implements LoginContract.ILoginView {


    private EditText accountUsername;
    private EditText accountPwd;
    private Button accountLoginBut;

//    @Override
//    public int addBarColor() {
//        return Color.WHITE;
//    }
//
//    @Override
//    public View addStatusView() {
//        return f(R.id.account_linear);
//    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_account;
    }

    @Override
    public void initView() {
        accountUsername = f(R.id.account_username);
        accountPwd = f(R.id.account_pwd);
        accountLoginBut = f(R.id.account_login_but);

        accountLoginBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    String nametv = accountUsername.getText().toString().trim();
                    String pwdtv = accountPwd.getText().toString().trim();
                    if (TextUtils.isEmpty(nametv)||TextUtils.isEmpty(pwdtv)){
                        Toast.makeText(getActivity(), "用户名密码不能为空", Toast.LENGTH_SHORT).show();
                        return;
                    }else {
                        p.getLogin(nametv,pwdtv,HttpCode.LOGINCODE);
                    }
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void inject() {
        DaggerLoginComponent.builder().loginModules(new LoginModules(this)).build().inject(this);
    }

    @Override
    public void showView(BaseEntity baseEntity) {
        Log.i("wxy", "showView: "+11);
        LogUtils.json(baseEntity);
        if (baseEntity.status.equals("200")){
            SpUtils.editLogin();
        }
        ResponseUserEntity responseUserEntity= (ResponseUserEntity) baseEntity;
        LogUtils.json(responseUserEntity);
        Intent intent = new Intent();
        intent.setAction(BroadAction.UPDATEUSERACTION);
        intent.putExtra("userValues",responseUserEntity.getValues());
        getActivity().sendBroadcast(intent);
        getActivity().finish();
    }

    @Override
    public void showHeadImg(BaseEntity baseEntity) {

    }
}